package com.xsd.juc;

import java.util.concurrent.*;

/**
 * @Description: 分支合并框架
 * @Author: zhoufuzong
 * @Created: 2021-01-03 18:26:02
 * @Version: 1.1.0
 */

class MyTask extends RecursiveTask<Integer>{
    public static final Integer ADJUST_VALUE=10;

    private int begin;
    private int end;
    private int result;

    public MyTask(int begin, int end) {
        this.begin=begin;
        this.end =end;
    }


    @Override
    protected Integer compute() {

        if ((end-begin)<=ADJUST_VALUE){
            for (int i = begin; i <=end ; i++) {
                result = result+i;

            }
        }else {
            int middle =(end+begin)/2;
            MyTask task01=new MyTask(begin,middle);
            MyTask task02 = new MyTask(middle+1,end);
            task01.fork();
            task02.fork();
            result = task01.join()+task02.join();
        }
        return result;
    }
}

public class ForkJoinTaskDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyTask myTask = new MyTask(0,100);
        ForkJoinPool threadPool = new ForkJoinPool();

        ForkJoinTask<Integer> forkJoinTask =threadPool.submit(myTask);
        System.out.println(forkJoinTask.get());

        threadPool.shutdown();
    }

}
